<template>
  <div class="adminSet">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="旧密码：" prop="oldPassword">
        <el-input
          type="password"
          v-model="ruleForm.oldPassword"
    
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码：" prop="newPassword">
        <el-input type="password" v-model="ruleForm.newPassword"></el-input>
      </el-form-item>
      <el-form-item label="确认密码：" prop="newPasswordone">
        <el-input
          type="password"
          v-model="ruleForm.newPasswordone"
          @blur="checknewPasswordone(ruleForm.newPasswordone)"
        ></el-input>
      </el-form-item>

      <el-form-item style="margin-left:100px">
        <el-button type="primary" @click="submitForm('ruleForm')" size="mini">保存</el-button>
        <!--
        <el-button @click="resetForm('ruleForm')" size="mini">取消</el-button>
        -->
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { changePassword } from "@/api/platformmanage";
import { _login } from "@/api/loginApi";
import qs from "qs";
export default {
  data() {
    return {
      ruleForm: {
        oldPassword: "",
        userId: "",
        newPassword: "",
        newPasswordone: ""
      },
      vv: "",
      account: "",
      userId: "",
      rules: {
        oldPassword: [
          { required: true, message: "请输入旧密码", trigger: "blur" }
        ],
        newPassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
        { required: true, message: "请输入新密码", trigger: "blur" }
        ],
        newPasswordone: [
          { required: true, message: "请再次输入密码", trigger: "blur" }
        ]

        // password: [{ required: true }]
      }
    };
  },
  mounted() {
    console.log(this.$route.params.edit, "llll");
    this.logininfo = localStorage.getItem("logininfo");
    this.userId = localStorage.getItem("username");
    this.ruleForm.userId = JSON.parse(this.logininfo).userId;
    this.account = JSON.parse(this.logininfo).account;
  },
  methods: {
    // checkpassword(v) {
    //   let data = {
    //     tenantId: "01",
    //     authType: 1,
    //     password: this.ruleForm.oldPassword,
    //     authId: this.account
    //   };
    //   _login(qs.stringify(data)).then(res => {
    //   console.log("change.res.code: " + res.code)
    //     if (res.code === 0) {
    //       this.$message({
    //         showClose: true,
    //         message: "旧密码不正确",
    //         type: "error"
    //       });
    //       this.ruleForm.oldPassword = "";
    //     }
    //   });
    // },
    checknewPasswordone(v) {
      this.vv = v;
      if (this.ruleForm.newPassword === this.vv) {
      } else {
        this.$message({
          showClose: true,
          message: "两次密码不一致",
          type: "error"
        });
      }
    },
    submitForm(formName) {
      if (
        this.ruleForm.oldPassword == "" ||
        this.ruleForm.oldPassword == null
      ) {
        this.$message.error("请输入旧密码");
        return;
      }
      if (
        this.ruleForm.newPassword == "" ||
        this.ruleForm.newPassword == null
      ) {
        this.$message.error("请输入新密码");
        return;
      }
      if (
        this.ruleForm.newPasswordone == "" ||
        this.ruleForm.newPasswordone == null
      ) {
        this.$message.error("请确认密码");
        return;
      }
      if (this.ruleForm.newPassword === this.vv) {
        let data = {
          userId: this.userId,
          oldPassword: this.ruleForm.oldPassword,
          newPassword: this.ruleForm.newPassword
        };
        changePassword(qs.stringify(data)).then(res => {
            if(res.code==1){
  this.$message({
            showClose: true,
            message: "修改密码成功,请重新登录",
            type: "success"
          });
          // this.$router.push("/login")
            }else{
                this.$message({
            showClose: true,
            message:res.message,
            type: "error"
          }); 
            }
        
        });
      } else {
        this.$message({
          showClose: true,
          message: "两次密码不一致",
          type: "error"
        });
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$router.push("accountList");
    }
  }
};
</script>>

<style lang="less" scoped>
.adminSet {
  border: 1px solid #ebeef5;
  margin-left: 150px;
  padding: 50px 0px 20px 80px;
  width: 60%;
}
.el-col-20 {
  border: 1px solid #ebeef5;
  margin-left: 150px;
  width: 60%;
}
.el-input {
  width: 60%;
}
</style>
